﻿using System;

namespace CSW.Framework.Linq.SchemaModel
{
    /// <summary>
    /// Table Property.
    /// </summary>
    public class TableProperty
    {
        private string m_DataType;
        private Type m_Type;

        private string m_PropertyName;

        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        /// <value>The name of the property.</value>
        public string PropertyName 
        {
            get { return m_PropertyName; }
            set
            {
                if (!string.IsNullOrEmpty(m_PropertyName))
                {
                    throw new Exception("Cannot changed PropertyName once its set.");
                }
                m_PropertyName = value;
            }
        }

        /// <summary>
        /// Gets or sets the column.
        /// </summary>
        /// <value>The column.</value>
        public TableColumn Column { get; set; }

        /// <summary>
        /// Gets or sets the child .NET data type.
        /// </summary>
        /// <value>The child .NET data type.</value>
        public string ChildDataType { get; set; }

        /// <summary>
        /// Gets or sets the name of the child column.
        /// </summary>
        /// <value>The name of the child column.</value>
        public string ChildColumnName { get; set; }

        /// <summary>
        /// Gets or sets the .NET data type.
        /// </summary>
        /// <value>The .NET data type.</value>
        public string DataType
        {
            get { return m_DataType; }
            set
            {
                m_DataType = value;

                Type type = Type.GetType(m_DataType, false);
                if (type != null)
                {
                    m_Type = type;
                }
                else
                {
                    if (!IsChildContainer && IsPrimitiveType)
                    {
                        throw new Exception(string.Format("Can't find type '{0}'.", m_DataType));
                    }
                }
            }
        }

        /// <summary>
        /// Gets the type.
        /// </summary>
        /// <value>The type.</value>
        public Type Type
        {
            get { return m_Type; }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is a child container.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is a child container; otherwise, <c>false</c>.
        /// </value>
        public bool IsChildContainer
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is a child list.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is a child list; otherwise, <c>false</c>.
        /// </value>
        public bool IsChildList
        {
            get; set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is a primitive type.
        /// </summary>
        /// <value><c>true</c> if this instance is a primitive type; otherwise, <c>false</c>.</value>
        public bool IsPrimitiveType
        {
            get; set;
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return string.Format("{0} {1}", DataType, PropertyName);
        }

        /// <summary>
        /// TEMP REMOVE
        /// </summary>
        /// <param name="p">The p.</param>
        public void SetPropertyName(string p)
        {
            m_PropertyName = p;
        }
    }
}
